package com.yixun.controller;

import com.yixun.entity.YxUser;
import com.yixun.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/user")
public class UserController extends BaseController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/getCurrentUser")
    @ResponseBody
    public Map<String,Object> getCurrentUser(){
        return userService.getUserBySession(this.getSession());
    }


    @RequestMapping(value = "/getUserMenuJsonTree")
    @ResponseBody
    public String getUserMenuJsonTree(){
        Map<String, Object> session = userService.getUserBySession(this.getSession());
        YxUser sessionUser = (YxUser) session.get("data");
        //System.out.println(userService.menuListToJsonStrByUserID(sessionUser.getUser_id()));
        return userService.menuListToJsonStrByUserID(sessionUser.getUser_id());
    }

    @RequestMapping(value = "/leftmenu")
    public String leftmenu(){
        return "org/leftmenu/publicMenu";
    }

    /**
     * 跳转用户列表页面
     */
    @RequestMapping(value = "/list")
    public String list(Model model){
        model.addAttribute("dept_id", this.getSessionUser().getDept_id());
        return "org/user/userList";
    }

    /**
     * 根据dept_id获取Dept列表
     */
    @RequestMapping(value = "/getUserList")
    @ResponseBody
    public List<YxUser> getUserList(@RequestBody Map<String, String> map){
        return userService.getUserList(map);
    }

    /**
     * 根据dept_id获取Dept列表条数
     */
    @RequestMapping(value = "/getUserCount")
    @ResponseBody
    public String getUserCount(@RequestBody Map<String, String> map){
        return userService.getUserCount(map);
    }

    /**
     * 跳向添加用户页面
     */
    @RequestMapping(value = "/go_user_addFdept")
    public String go_user_addFdept(String dept_id, String user_id, Model model){
        model.addAttribute("dept_id", dept_id);
        model.addAttribute("user_id", user_id);
        return "/org/user/user_addFdept";
    }

    /**
     * 添加用户
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public boolean add(@RequestBody YxUser user){
        int row = userService.insertUser(this.getSessionUser(), user);
        return row == 1;
    }

    /**
     * 根据user_id获取UserInfo
     */
    @RequestMapping(value = "/getUserBean/{user_id}")
    @ResponseBody
    public YxUser getUserBean(@PathVariable(value = "user_id") String user_id){
        return userService.getUserBean(user_id);
    }

    /**
     * 修改用户信息
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public boolean edit(@RequestBody YxUser user){
        int row = userService.updateUser(this.getSessionUser(), user);
        return row == 1;
    }

    /**
     * 删除用户信息
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ResponseBody
    public boolean del(@RequestBody Map map){
        int row = userService.delUser(this.getSessionUser(), map);
        return row > 0;
    }

    /**
     * 移动用户
     */
    @RequestMapping(value = "/move", method = RequestMethod.POST)
    @ResponseBody
    public boolean move(@RequestBody Map map){
        int row = userService.moveUser(this.getSessionUser(), map);
        return row > 0;
    }

    /**
     * 账号 启用/停用
     */
    @RequestMapping(value = "/setUserStatus", method = RequestMethod.POST)
    @ResponseBody
    public boolean setUserStatus(@RequestBody Map map){
        int row = userService.setUserStatus(map);
        return row > 0;
    }

    /**
     * 跳向修改密码页面
     */
    @RequestMapping(value = "/go_reload_pw")
    public String go_reload_pw(@RequestParam(value = "user_id") String user_id, Model model){
        model.addAttribute("user_id", user_id);
        return "org/user/reload_pw";
    }

    /**
     * 修改用户密码
     */
    @RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
    @ResponseBody
    public boolean resetPassword(@RequestBody Map map){
        int row = userService.resetPassword(this.getSessionUser(), map);
        return row == 1;
    }

    /**
     * 获取所有用户信息
     */
    @RequestMapping(value = "/getAllUserList")
    @ResponseBody
    public List<YxUser> getAllUserList(){
        return userService.getAllUserList();
    }


    @RequestMapping(value = "/update_user")
    public String update_user(Model model){
        model.addAttribute("user_id", this.getSessionUser().getUser_id());
        return "org/user/update_user";
    }

    /**
     * 更新个人信息
     */
    @RequestMapping(value = "/editSimple", method = RequestMethod.POST)
    @ResponseBody
    public boolean editSimple(@RequestBody YxUser user){
        int row = userService.updateSimple(this.getSessionUser(), user);
        return row == 1;
    }

    /**
     * 更新个人密码
     */
    @RequestMapping(value = "/update_userpw")
    public String update_userpw(Model model){
        model.addAttribute("user_id", this.getSessionUser().getUser_id());
        return "org/user/update_userpw";
    }

    /**
     * 验证原始密码
     */
    @RequestMapping(value = "/checkOldPw", method = RequestMethod.POST)
    @ResponseBody
    public boolean checkOldPw(@RequestBody Map<String, String> map){
        YxUser user = userService.checkOldPw(this.getSessionUser().getUser_name(), map);
        return ObjectUtils.isEmpty(user);
    }

}
